package package01;

import java.sql.*;

/**
 * @Description JDBC简单应用
 * @Author zzj
 * @Date 2020/7/16 13:05
 */
public class Jdbc {
    public static void main(String[] args) {
        // 4大参数
        String driverClassName = "oracle.jdbc.OracleDriver";
        String url = "jdbc:oracle:thin:@39.105.173.35:1521:helowin";
        String username = "scott";
        String passwd = "123456";

        Connection conn = null;
        // 加载驱动
        try {
            Class.forName(driverClassName);
            // 创建会话
            conn = DriverManager.getConnection(url, username, passwd);
            // 声明sql语句
            String sql = "select * from emp";
            // 预编译
            Statement statement = conn.createStatement();
            // 执行SQL语句,返回结果集
            ResultSet rs = statement.executeQuery(sql);
            // 遍历结果集
            while (rs.next()) {
                System.out.println(rs.getInt(1) + "\t" + rs.getString(2));
            }
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
